package myky.com.doctorapp.presenter

import io.reactivex.disposables.Disposable
import myky.com.doctorapp.api.ResponseTransformer
import myky.com.doctorapp.api.schedulers.SchedulerHelper
import myky.com.doctorapp.base.BasePresenterIm
import myky.com.doctorapp.base.SimpleSubscribe
import myky.com.doctorapp.contract.FindDoctorByAreaContract
import myky.com.doctorapp.entity.AreaWrapperEntity
import myky.com.doctorapp.model.FindDoctorByAreaModel
import myky.com.doctorapp.utils.ILog

class FindDoctorByAreaPresenter : BasePresenterIm<FindDoctorByAreaContract.View>(), FindDoctorByAreaContract.Presenter {

    private var findDoctorByAreaModel: FindDoctorByAreaModel = FindDoctorByAreaModel()


    override fun findArea() {

        findDoctorByAreaModel.findArea()
                .compose<AreaWrapperEntity.DataBean>(ResponseTransformer.handleResult<AreaWrapperEntity.DataBean>())
                .compose<AreaWrapperEntity.DataBean>(SchedulerHelper.applySchedulers<AreaWrapperEntity.DataBean>())
                .subscribe(object : SimpleSubscribe<AreaWrapperEntity.DataBean>() {
                    override fun onSubscribe(d: Disposable) {
                        addSubscription(d)
                        mView.showLoadingView("请稍后")
                    }

                    override fun onFailed(msg: String?) {
                        ILog.i(msg)
                        mView.hideLoading()
                    }

                    override fun onSuccess(t: AreaWrapperEntity.DataBean?) {

                        mView.hideLoading()

                        t?.let {
                            it.area[0].isSelected = true

                            mView.showArra(it.area)
                        }

                    }
                })

    }
}
